<?php
/**
 * 
 * @author huangjian E-mail:870596179@qq.com
 * @version 创建时间：2016年10月21日 上午10:42:40 
 * 类说明
 */
class AdminUserAction extends BaseAction{
	
	/**
	 * @authority 浏览
	 */
	public function index(){
		$this->display('index');
	}
	
	//列表
	public function getAllData(){
		$Model = M($this->getActionName());
		$map = $this->loadSeachCondition();
		$map['flag'] = 1;
		$total = $Model->where($map)->count();// 查询满足要求的总记录数
		$page = json_decode($this->pageParam($total));
		$data = $Model->where($map)->limit($page->firstRow,$page->listRows)->order($page->sort)->select();
		$vo = $this->toJosnForGird($data,$page);
		echo json_encode($vo);
	}
	
	/**
	 * 组装条件
	 * @return multitype:multitype:string
	 */
	public function loadSeachCondition(){
		$map = array();
		/*
		if(!empty($_GET['field'])){
			$map['field'] = array("like","%".$_GET['field']."%");
		}
		*/
		return $map;
	}
	
	// 新增
	public function add(){
		$Model = M('Auth_group');
		$map['status'] = 1;
		$vo = $Model->where($map)->select();
		$this->assign('vo',$vo);
		$this->display('add');
	}
	
	//插入
	public function insert(){
		$Model = M($this->getActionName());
		unset ( $_POST [$Model->getPk()] );
		if (false === $Model->create()) {
			$this->error($Model->getError());
		}
		if(!empty($_REQUEST['groups'])) {
			$groups = explode(",", $_REQUEST['groups']);
		}
		//保存当前数据对象
		if ($result = $Model->add()) { //保存成功
			//操作权限关联表
			$AuthGroupAccess = M('Auth_group_access');
			$arr = Array();
			if(!empty($groups)) {
				for($i=0; $i<count($groups); $i++) {
					$arr[$i]['uid'] = $result;
					$arr[$i]['group_id'] = $groups[$i];
				}
			}
			if(!empty($arr)) {
				$AuthGroupAccess->addAll($arr);
			}
			$this->ajaxReturn($data,L('ADMIN_ADD_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_ADD_ERROR'),0);
		}
	}
	
	// 修改
	public function edit(){
		//获取信息
		$Model = M($this->getActionName());
		$where['id'] = $_REQUEST['id'];
		$vo = $Model->where($where)->find();
		//获取所有权限角色
		$AuthGroupModel = M('Auth_group');
		$map['status'] = 1;
		$groupList = $AuthGroupModel->where($map)->select();
		//获取当前用户拥有权限
		$AuthGroupAccessModel = M('Auth_group_access');
		$condition['uid'] = $_REQUEST['id'];
		$userGroups = $AuthGroupAccessModel->where($condition)->getfield("group_concat(group_id)");
		$userGroups = explode(',',$userGroups);
		$this->assign('vo',$vo);
		$this->assign('groupList',$groupList);
		$this->assign('userGroups',$userGroups);
		$this->display('edit');
	}
	
	//更新
	public function update() {
		$Model = D($this->getActionName());
		if(false === $Model->create()) {
			$this->error($Model->getError());
		}
		$data = $Model->create();
		if(!empty($_REQUEST['groups'])) {
			$groups = explode(",", $_REQUEST['groups']);
		}
		// 更新数据
		if(false !== $Model->save()) {
			//操作权限关联表
			$AuthGroupAccess = M('Auth_group_access');
			$map['uid'] = $data['id'];
			$AuthGroupAccess->where($map)->delete();
			$arr = Array();
			if(!empty($groups)) {
				for($i=0; $i<count($groups); $i++) {
					$arr[$i]['uid'] = $data['id'];
					$arr[$i]['group_id'] = $groups[$i];
				}
			}
			if(!empty($arr)) {
				$AuthGroupAccess->addAll($arr);
			}
			$this->ajaxReturn(1,L('ADMIN_EDIT_SUCCESS'),1);
		} else {
			$this->ajaxReturn(0,L('ADMIN_EDIT_ERROR'),0);
		}
	}
	
	// 修改密码
	public function editPwd(){
		$Model = M($this->getActionName());
		$where['id'] = $_REQUEST['id'];
		$vo = $Model->where($where)->find();
		$this->assign('vo',$vo);
		$this->display();
	}
	
	//检查名字是否可注册
	public function checkName(){
		$username = $_REQUEST['param'];
		$user = M($this->getActionName());
		$where['username'] = $username;
		$count=$user->where($where)->count();
		if($count){
			echo '用户名已被注册！';
		}else{
			echo 'y';
		}
	}
	
	//检查名字是否可注册，排除本账户
	public function exCheckName(){
		$username = $_REQUEST['param'];
		$user = M($this->getActionName());
		if(!empty($_REQUEST['id'])){
			$map['id'] = $_REQUEST['id'];
			$result = $user->where($map)->find();
			if($username == $result['username']) {
				echo 'y';
				return;
			}else{
				$where['username'] = $username;
			}
		}else{
			$where['username'] = $username;
		}
		$count=$user->where($where)->count();
		if($count){
			echo '用户名已被注册！';
		}else{
			echo 'y';
		}
	}
	
}